﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TL_Cloud.Core.Entities;
using TL_Cloud.Core.Services;
using TL_Cloud.Models.BookShop;
using TL_Cloud.RepositoryInterface.BookShop;
using TL_Cloud.ViewModels.BookShop.BKPics;

namespace TL_Cloud.RepositoryService.BookShop
{
   public class BKPicsRepository : BaseRepository<Bk_Pics>, IBKPicsRepository
    {
        public BKPicsRepository(AppBookShopContext myContext,
       IPropertyMappingContainer propertyMappingContainer) : base(myContext, propertyMappingContainer)
        {
            currentDbSet = _myContext.BkPics;
        }

        public async Task<PaginatedList<Bk_Pics>> GetAllEntitiesAsync(BKPicsQueryParameters parameters)
        {
            var query = currentDbSet.AsQueryable();

            if (!string.IsNullOrEmpty(parameters.BookId))
            {
                string BookId = parameters.BookId.ToLowerInvariant();
                query = query.Where(n => n.BookId.ToLowerInvariant() == BookId);
            }

            if (!string.IsNullOrEmpty(parameters.PicName))
            {
                string PicName = parameters.PicName.ToLowerInvariant();
                query = query.Where(n => n.PicName.ToLowerInvariant() == PicName);
            }
            if (!string.IsNullOrEmpty(parameters.PicUrl))
            {
                string PicUrl = parameters.PicUrl.ToLowerInvariant();
                query = query.Where(n => n.PicUrl.ToLowerInvariant() == PicUrl);
            }

            int count = 0;
            List<Bk_Pics> data = new List<Bk_Pics>();
            if (parameters.isPage)
            {
                count = await query.CountAsync();
                data = await query
                    .Skip(parameters.PageIndex * parameters.PageSize)
                    .Take(parameters.PageSize)
                    .ToListAsync();
            }
            else
            {
                data = await query.ToListAsync();
            }
            return new PaginatedList<Bk_Pics>(parameters.PageIndex, parameters.PageSize, count, data);
        }

    }
}
